package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hsjk.szwj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hsjk.szwj.framework.security.core.util.SecurityFrameworkUtils;
import com.hsjk.szwj.framework.tenant.core.aop.TenantIgnore;
import com.hsjk.szwj.framework.tenant.core.context.TenantContextHolder;
import com.hsjk.szwj.module.train.controller.app.vo.collect.AppMemberCourseCollectRecordCreateOrUpdateReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCollectActionChapterRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCollectQuestionReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCollectQuestionRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCourseCollectQuestionReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCourseErrorQuestionReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberErrorActionChapterReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberErrorActionChapterRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberErrorQuestionRespVO;
import com.hsjk.szwj.module.train.convert.QuestionChapterConvert;
import com.hsjk.szwj.module.train.dal.dataobject.collect.MemberCourseCollectRecordDO;
import com.hsjk.szwj.module.train.dal.dataobject.question.QuestionChapterDO;
import com.hsjk.szwj.module.train.dal.mysql.collect.IMemberCourseCollectRecordMapper;
import com.hsjk.szwj.module.train.dal.mysql.question.IMemberQuestionRecordMapper;
import com.hsjk.szwj.module.train.dal.mysql.question.IQuestionChapterMapper;
import com.hsjk.szwj.module.train.service.IMemberRecordService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/11/03 17:14
 **/
@Service
public class MemberRecordServiceImpl implements IMemberRecordService {
    @Resource
    private IMemberCourseCollectRecordMapper courseCollectRecordMapper;

    @Resource
    private IMemberQuestionRecordMapper memberQuestionRecordMapper;

    @Resource
    private IQuestionChapterMapper questionChapterMapper;

    @Value("${szwj.platform-tenant-id}")
    private String platformTenantId;

    @Override
    public Boolean createOrUpdateMemberCollectCourseRecord(AppMemberCourseCollectRecordCreateOrUpdateReqVO reqVO) {
        List<AppMemberCourseCollectRecordCreateOrUpdateReqVO.MemberCollectRecordInfo> collectRecordInfos = reqVO.getCollectRecordInfos();
        List<MemberCourseCollectRecordDO> addList = new ArrayList<>();
        List<Long> deleteIdList = new ArrayList<>();
        if (CollUtil.isNotEmpty(collectRecordInfos)) {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            for (AppMemberCourseCollectRecordCreateOrUpdateReqVO.MemberCollectRecordInfo recordInfo : collectRecordInfos) {
                MemberCourseCollectRecordDO memberCourseCollectRecordDO = courseCollectRecordMapper
                        .selectOne(new LambdaQueryWrapperX<MemberCourseCollectRecordDO>().eq(MemberCourseCollectRecordDO::getQuestionId, recordInfo.getQuestionId()));
                if (ObjectUtil.isNull(memberCourseCollectRecordDO)) {
                    MemberCourseCollectRecordDO recordDO = new MemberCourseCollectRecordDO();
                    recordDO.setCourseId(reqVO.getCourseId());
                    recordDO.setQuestionId(recordInfo.getQuestionId());
                    recordDO.setMemberId(loginUserId);
                    addList.add(recordDO);
                } else {
                    if (!recordInfo.getIfCollect()) {
                        deleteIdList.add(memberCourseCollectRecordDO.getId());
                    }
                }
            }
            if (CollUtil.isNotEmpty(addList)) {
                courseCollectRecordMapper.insertBatch(addList);
            }
            if (CollUtil.isNotEmpty(deleteIdList)) {
                courseCollectRecordMapper.deleteBatchIds(deleteIdList);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    @TenantIgnore
    public List<AppMemberErrorQuestionRespVO> getMemberChapterErrorQuestionByCourseId(Long courseId) {
        List<Long> tenantIds = new ArrayList<>();
        Long tenantId = TenantContextHolder.getTenantId();
        if (ObjectUtil.isNotNull(tenantId)) {
            tenantIds.add(tenantId);
        }
        if (ObjectUtil.isNotNull(platformTenantId)) {
            tenantIds.add(Long.valueOf(platformTenantId));
        }
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        List<AppMemberErrorQuestionRespVO> memberChapterErrorQuestionByCourseList
                = memberQuestionRecordMapper.getMemberChapterErrorQuestionByCourseId(courseId, loginUserId,tenantIds);
        if (CollUtil.isNotEmpty(memberChapterErrorQuestionByCourseList)) {
            for (AppMemberErrorQuestionRespVO vo : memberChapterErrorQuestionByCourseList) {
                QuestionChapterDO questionChapterDO = questionChapterMapper.selectById(vo.getChapterId());
                if (ObjectUtil.isNotNull(questionChapterDO)) {
                    vo.setChapterName(questionChapterDO.getChapterName());
                }
            }
        }
        return memberChapterErrorQuestionByCourseList;
    }

    @Override
    @TenantIgnore
    public List<AppMemberCollectQuestionRespVO> getMemberChapterCollectQuestionByCourseId(Long courseId) {
        List<Long> tenantIds = new ArrayList<>();
        Long tenantId = TenantContextHolder.getTenantId();
        if (ObjectUtil.isNotNull(tenantId)) {
            tenantIds.add(tenantId);
        }
        if (ObjectUtil.isNotNull(platformTenantId)) {
            tenantIds.add(Long.valueOf(platformTenantId));
        }
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return memberQuestionRecordMapper.getMemberChapterCollectQuestionByCourseId(courseId, loginUserId,tenantIds);
    }

    @Override
    public List<AppMemberErrorActionChapterRespVO> getMemberErrorChapterByCourseId(AppMemberErrorActionChapterReqVO reqVO) {
        List<AppMemberErrorActionChapterRespVO> respVOList = new ArrayList<>();
        List<AppMemberErrorQuestionRespVO> memberChapterErrorQuestionByCourseId = this.getMemberChapterErrorQuestionByCourseId(reqVO.getCourseId());
        if (CollUtil.isNotEmpty(memberChapterErrorQuestionByCourseId)) {
            for (AppMemberErrorQuestionRespVO errorQuestion : memberChapterErrorQuestionByCourseId) {
                QuestionChapterDO questionChapterDO = questionChapterMapper.selectById(errorQuestion.getChapterId());
                AppMemberErrorActionChapterRespVO appMemberErrorActionChapterRespVO = QuestionChapterConvert.INSTANCE.convertAppMemberErrorActionChapterRespVO(questionChapterDO);
                respVOList.add(appMemberErrorActionChapterRespVO);
            }
        }
        return respVOList;
    }

    @Override
    public List<AppMemberCollectActionChapterRespVO> getMemberCollectChapterByCourseId(AppMemberCollectQuestionReqVO reqVO) {
        List<AppMemberCollectActionChapterRespVO> respVOList = new ArrayList<>();
        List<AppMemberCollectQuestionRespVO> memberChapterCollectQuestionByCourseId = this.getMemberChapterCollectQuestionByCourseId(reqVO.getCourseId());
        if (CollUtil.isNotEmpty(memberChapterCollectQuestionByCourseId)) {
            for (AppMemberCollectQuestionRespVO collectQuestion : memberChapterCollectQuestionByCourseId) {
                QuestionChapterDO questionChapterDO = questionChapterMapper.selectById(collectQuestion.getChapterId());
                AppMemberCollectActionChapterRespVO appMemberCollectActionChapterRespVO = QuestionChapterConvert.INSTANCE.convertAppMemberCollectActionChapterRespVO(questionChapterDO);
                respVOList.add(appMemberCollectActionChapterRespVO);
            }
        }
        return respVOList;
    }

    @Override
    @TenantIgnore
    public List<Long> getMemberCourseErrorQuestionIdList(AppMemberCourseErrorQuestionReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return memberQuestionRecordMapper.getMemberCourseErrorQuestionIdList(reqVO, loginUserId);
    }

    @Override
    public List<Long> getMemberCourseCollectQuestionIdList(AppMemberCourseCollectQuestionReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return memberQuestionRecordMapper.getMemberCourseCollectQuestionIdList(reqVO, loginUserId);
    }
}
